﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Icst.Leaflike.Data;
using System.Web.UI.WebControls;
using Icst.Leaflike.Web.controls;


namespace Icst.Leaflike.Web
{
    public abstract class pagesearch : pagebase
    {
        private SearchQuery _searchQuery = null;
        protected SearchQuery SearchQuery 
        { 
            get 
            {
                if (_searchQuery == null)
                {
                    _searchQuery = new SearchQuery();
                    foreach (var name in Enum.GetValues(typeof(SearchOption)))
                    {
                        string value = this.Para(name.ToString().ToLower(), string.Empty);
                        _searchQuery[(SearchOption)name] = value;
                    }
                    return _searchQuery;
                }

                return _searchQuery;
            } 
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnLoad();
            }
        }
        protected void nextButton_Click(object sender, EventArgs e)
        {
            string nextUrl = OnSave(false);
            PageExtension.Redirect(this, nextUrl + "?" + SearchQuery.ToString());
        }
        protected void ignoreButton_Click(object sender, EventArgs e)
        {
            string nextUrl = OnSave(true);
            PageExtension.Redirect(this, nextUrl+"?"+SearchQuery.ToString());
        }
        protected void AddListbox(DropDownList cbl, string parentAttr)
        {
            int index = 0;
            foreach (tree_attr attr in Tree.Instance.Attributes(parentAttr, Version))
            {
                ListItem li = new ListItem()
                {
                    Text = attr.imagelink,
                    Value = attr.attr_value,
                    Selected = (index== 0)
                };
                cbl.Items.Add(li);
                index++;
            }
        }
        protected void AddOptionsList(RadioButtonList cbl, string parentAttr)
        {
            int index = 0;
            foreach (tree_attr attr in Tree.Instance.Attributes(parentAttr, Version))
            {
                ListItem li = new ListItem()
                {
                    Text = attr.imagelink,
                    Value = attr.attr_no,
                    Selected = (index == 0)
                };
                cbl.Items.Add(li);
                index++;
            }
        }
        protected void AddSelection(OptionSelector selector, Dictionary<string,string> attrs)
        {
            int index = 0;
            foreach (var attr in attrs)
            {
                ListItem li = new ListItem()
                {
                    Text = attr.Value,
                    Value = attr.Key,
                    Selected = (index == 0)
                };
                selector.AddItems(li);
                index++;
            }
        }
        protected void AddSelection(OptionSelector selector, params tree_attr[] attrs)
        {
            int index = 0;
            foreach (tree_attr attr in attrs)
            {
                ListItem li = new ListItem()
                {
                    Text = attr.imagelink,
                    Value = attr.attr_no,
                    Selected = (index == 0)
                };
                selector.AddItems(li);
                index++;
            }
        }
        protected void AddSelection(OptionSelector selector,string parentAttr)
        {
            AddSelection(selector, Tree.Instance.Attributes(parentAttr, Version).ToArray());
        }

        protected abstract void OnLoad();
        protected abstract string OnSave(bool empty);
    }
}
